<?php
require_once (dirname(__FILE__) .'/PtObjectAbstract.php');
class PtDefaultStyle extends PtObjectAbstract
{
	public $id;
	public $id_project;
	public $selector;
	public $style;
	
	/**
	 * @see ObjectAbstract::$definition
	 */
	public static $definition = array(
		'table'   => 'custom_style',
		'primary' => 'id_style',
		'fields'  => array(
			'id_project' => array('type' => self::TYPE_STRING, 'validate' => 'isUnsignedId', 'required' => true),
			'selector'   => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true),
			'style'      => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'required' => true),
		)
	);
	
	public static function getByIdProjectAndSelector ($id_project, $selector)
	{
		if (!self::$db)
			self::$db = DbAbstract::getInstance();
		
		$sql = new PtDbQuery();
		$sql->from(self::$definition['table'], 'a');
		$sql->where('a.id_project = '. (int)$id_project .' AND a.selector = \''. $selector .'\'');
		
		$result = self::$db->getRow($sql, true, false);
		
		if ($result)
		{
			return new self($result[self::$definition['primary']]);
		}
		else
		{
			$item = new self();
			$item->id_project = $id_project;
			$item->selector   = $selector;
			return $item;
		}
	}
}